<template>
  <div class="app-container">
    <h2 style="text-align: center">字体和七步法</h2>

    <el-steps
      :active="2"
      process-status="wait"
      align-center
      style="margin-bottom: 40px"
    >
      <el-step title="添加字体" />
      <el-step title="添加七步" />
    </el-steps>
    <div>
      <el-form :inline="true" :model="dataForm">
        <el-form-item>
          <el-select
            v-model="buzhou"
            placeholder="请选择类型"
            @change="buzhouChange"
          >
            <el-option label="欣赏" :value="0"> </el-option>
            <el-option label="认读" :value="1"> </el-option>
            <el-option label="观察" :value="2"> </el-option>
            <el-option label="分析" :value="4"> </el-option>
            <el-option label="笔法" :value="3"> </el-option>
            <el-option label="视频" :value="7"> </el-option>
            <el-option label="对比" :value="5"> </el-option>
            <el-option label="随堂练习" :value="6"> </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <el-form :rules="dataRule" ref="wordForm" :model="word" label-width="120px">
      <el-form-item label="视频解析" prop="videoDescription">
        <el-input
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 4 }"
          placeholder="请输入笔画特征"
          v-model="word.videoDescription"
        >
        </el-input>
      </el-form-item>
      <el-form-item label="背景音乐" prop="bgm">
        <el-upload
          class="upload-uploader"
          :action="'http://121.41.167.195:8000/api/oss/upload'"
          :on-success="handleAudioSuccess"
          :before-upload="beforeAudioUpload"
          :show-file-list="false"
        >
          <audio :src="word.bgm" controls="controls">
            您的浏览器不支持音频播放
          </audio>
        </el-upload>
      </el-form-item>
      
      <el-form-item label="视频解析音频" prop="videoSounds">
        <el-upload
          class="upload-uploader"
          :action="'http://121.41.167.195:8000/api/oss/upload'"
          :on-success="handleAudioSuccess1"
          :before-upload="beforeAudioUpload"
          :show-file-list="false"
        >
          <audio :src="word.videoSounds" controls="controls">
            您的浏览器不支持音频播放
          </audio>
        </el-upload>
      </el-form-item>
      <el-form-item label="视频" prop="video">
        <el-upload
          :on-success="handleVodUploadSuccess"
          :action="'http://121.41.167.195:8000/api/oss/upload'"
          class="upload-uploader"
          :before-upload="beforeVideoUpload"
          :show-file-list="false"
        >
          <video :src="word.video" controls="controls" class="avatar">
            您的浏览器不支持视频播放
          </video>
        </el-upload>
      </el-form-item>
      <el-form-item label="视频">
        <el-upload
          :on-success="handleVodUploadSuccess2"
          :action="'http://121.41.167.195:8000/api/oss/upload'"
          class="upload-uploader"
          :before-upload="beforeVideoUpload"
          :show-file-list="false"
        >
          <video :src="word.video2" controls="controls" class="avatar">
            您的浏览器不支持视频播放
          </video>
        </el-upload>
      </el-form-item>
      <el-form-item label="视频">
        <el-upload
          :on-success="handleVodUploadSuccess3"
          :action="'http://121.41.167.195:8000/api/oss/upload'"
          class="upload-uploader"
          :before-upload="beforeVideoUpload"
          :show-file-list="false"
        >
          <video :src="word.video3" controls="controls" class="avatar">
            您的浏览器不支持视频播放
          </video>
        </el-upload>
      </el-form-item>
    </el-form>

    <div>
      <el-button @click="previous">上一步</el-button>
      <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate()"
        >保存</el-button
      >
    </div>
  </div>
</template>

<script>
import Vue from "vue";
export default {
  data() {
    return {
      buzhou: 7,
      dataForm: { name: ""},
      wordLoading: false,
      saveBtnDisabled: false, // 保存按钮是否禁用
      word: {
        // 出版社对象
        video: "",
        video2: "",
        video3: "",
        videoDescription: "",
        videoSounds: "",
        bgm: "https://bby-0902.oss-cn-beijing.aliyuncs.com/2021/05/134880a6a8d82349c09412bfec8fcd2113视频背景音乐.mp3",
        id: null,
      },
      type: 1, // 0:添加1:修改
      saveWordBtnDisabled: false,
      dataRule: {
        videoDescription: [
          { required: true, message: "视频解析不能为空", trigger: "blur" },
        ],
        video: [{ required: true, message: "视频不能为空" }],
        video2: [{ required: true, message: "视频不能为空" }],
        video3: [{ required: true, message: "视频不能为空" }],
        bgm: [{ required: true, message: "背景音乐不能为空" }],
        videoSounds: [{ required: true, message: "视频解析音频不能为空" }],
      },
    };
  },

  activated() {
    this.$refs.wordForm.resetFields();
    this.buzhou = 7;
    // 传的就是wordId
    this.word.id = this.$route.params.id;
    // 有数据就要回显数据
    this.getWordList();
  },

  methods: {
    beforeVideoUpload(file) {
      const fileName = file.name;
      const fileType = fileName.substring(fileName.lastIndexOf("."));
      if (fileType !== ".mp4" && fileType !== ".flv") {
        this.$message.error("上传视频频只能是mp4/flv!");
        return false;
      }
      return true;
    },
    beforeAudioUpload(file) {
      const fileName = file.name;
      const fileType = fileName.substring(fileName.lastIndexOf("."));
      if (fileType !== ".mp3") {
        this.$message.error("上传音频只能是mp3!");
        return false;
      }
      return true;
    },
    handleAudioSuccess(response, file, fileList) {
      // 上传视频的url
      this.word.bgm = response.url;
    },
    handleAudioSuccess1(response, file, fileList) {
      // 上传视频的url
      this.word.videoSounds = response.url;
    },
    handleVodUploadSuccess(response, file, fileList) {
      // 上传视频的url
      this.word.video = response.url;
    },
    handleVodUploadSuccess2(response, file, fileList) {
      // 上传视频的url
      this.word.video2 = response.url;
    },
    handleVodUploadSuccess3(response, file, fileList) {
      // 上传视频的url
      this.word.video3 = response.url;
    },
    buzhouChange(value) {
      switch (value) {
        case 0:
          // 欣赏
          this.$router.push({
            path: "/ruanbi/ruanbixsAdd/" + this.$route.params.id,
          });
          break;
        case 1:
          // 认读
          this.$router.push({
            path: "/ruanbi/ruanbirdAdd/" + this.$route.params.id,
          });
          break;
        case 2:
          // 观察
          this.$router.push({
            path: "/ruanbi/ruanbigcAdd/" + this.$route.params.id,
          });
          break;
        case 3:
          // 笔法
          this.$router.push({
            path: "/ruanbi/ruanbibfAdd/" + this.$route.params.id,
          });
          break;
        case 4:
          // 分析
          this.$router.push({
            path: "/ruanbi/ruanbifxAdd/" + this.$route.params.id,
          });
          break;
        case 5:
          // 对比
          this.$router.push({
            path: "/ruanbi/ruanbidbAdd/" + this.$route.params.id,
          });
          break;
        case 6:
          // 随堂练习
          this.$router.push({
            path: "/ruanbi/ruanbistlxAdd/" + this.$route.params.id,
          });
          break;
        default:
          // 视频
          this.$router.push({
            path: "/ruanbi/wordVideo/" + this.$route.params.id,
          });
          break;
      }
    },
    getWordList() {
      this.$http({
        url: this.$http.adornUrl("/sf/cbtypeface/info/" + this.$route.params.id),
        method: "get",
      }).then(({ data }) => {
        this.$refs.wordForm.resetFields();
        this.word.id = this.$route.params.id;
        if (data && data.code === 0) {
          this.word = data.cbTypeface;
        }
      });
    },
    // 章节对话框中的保存或修改
    saveOrUpdate() {
      this.updateWord();
    },
    // 更新章节图片信息
    updateWord() {
      this.word.bgm = "https://bby-0902.oss-cn-beijing.aliyuncs.com/2021/05/134880a6a8d82349c09412bfec8fcd2113视频背景音乐.mp3";
      this.$refs["wordForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sf/cbtypeface/update`),
            method: "post",
            data: this.$http.adornData(
              {
                id: this.word.id,
                video: this.word.video,
                video2: this.word.video2,
                video3: this.word.video3,
                videoSounds: this.word.videoSounds,
                bgm: this.word.bgm,
                videoDescription: this.word.videoDescription,
              },
              false
            ),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "修改成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getWordList();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    // 打开对话框，添加、修改
    addWord(status, id) {
      // 重置表单 应该只是把表单有的清空而已
      this.$nextTick(() => {
        this.$refs.wordForm.resetFields();
      });
      if (status == 0) {
        // 添加
        this.type = 0;
        // 清空
        this.word.id = null;
      } else {
        // 修改
        this.type = 1;
        // 回显数据
        this.$http({
          url: this.$http.adornUrl("/sf/cbtypeface/info/" + id),
          method: "get",
        }).then(({ data }) => {
          this.word.id = data.cbTypeface.id;
          this.word.name = data.cbTypeface.name;
        });
      }
      this.dialogWordVisible = true;
    },
    // 表单提交
    wordFormSubmit() {
      // 表单验证
      this.$refs["wordForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sf/cbtypeface/save`),
            method: "post",
            data: this.$http.adornData(
              {
                name: this.word.name,
              },
              false
            ),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "添加成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  // 刷新
                  this.getWordList();
                  // 只能添加一次
                  this.xzBtn = true;
                  this.dialogWordVisible = false;
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    previous() {
      this.$router.push({
        path: "/ruanbi/word7",
      });
    },
    // 通过章节id删除章节
    deleteById(id) {
      this.$confirm(`确定对[字体id=${id}]进行[删除]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/sf/cbtypeface/delete"),
            method: "post",
            data: this.$http.adornData([id], false),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "删除成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getWordList();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },
  },
};
</script>
<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.chanpterList {
  position: relative;
  list-style: none;
  margin: 0;
  padding: 0;
}
.chanpterList li {
  position: relative;
}
.chanpterList p {
  float: left;
  font-size: 20px;
  margin: 10px 0;
  padding: 10px;
  height: 70px;
  line-height: 50px;
  width: 100%;
  border: 1px solid #ddd;
}
.chanpterList .acts {
  float: right;
  font-size: 14px;
}

.videoList {
  padding-left: 50px;
}
.videoList p {
  float: left;
  font-size: 14px;
  margin: 10px 0;
  padding: 10px;
  height: 50px;
  line-height: 30px;
  width: 100%;
  border: 1px dotted #ddd;
}
</style>
